package com.client.ryt.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.client.ryt.R;
import com.client.ryt.adapter.TradeRecordAdapter;
import com.client.ryt.basic.BaseActivity;
import com.client.ryt.basic.BaseException;
import com.client.ryt.basic.BaseLiteners;
import com.client.ryt.basic.BaseResponse;
import com.client.ryt.basic.BaseTask;
import com.client.ryt.config.LoginConfig;
import com.client.ryt.entity.biz.TradeRecordEntity;
import com.client.ryt.entity.parameter.TradeRecordParameters;
import com.client.ryt.task.http.request.TradeRecordRequest;
import com.client.ryt.task.http.response.TradeRecordResponse;
public class TradeRecordListActivity extends BaseActivity implements AdapterView.OnItemClickListener{
    TradeRecordListActivity oThis = this;
    ListView lv = null;
    TradeRecordAdapter adapter;

    int pagesize = 8;
    int pageindex =1;
    int pagecount = 0;
    Boolean isLastPage = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trade_record_list);
        lv = (ListView)findViewById(R.id.lv_traderecord);
        lv.setOnScrollListener(new OnScrollListenerImple());
        lv.setOnItemClickListener(this);
        loadData();
    }


    private class OnScrollListenerImple implements AbsListView.OnScrollListener {
        @Override
        public void onScroll(AbsListView listView, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
            int lastItem = firstVisibleItem + visibleItemCount;
            if(lastItem == totalItemCount) {
                System.out.println("Scroll to the listview last item");
                View lastItemView=(View) listView.getChildAt(listView.getChildCount()-1);
                if(lastItemView != null) {
                    if ((listView.getBottom()) == lastItemView.getBottom()) {
                        System.out.println("========Scroll to the listview bottom =============");
                        loadData();
                    }
                }
            }
        }
        @Override
        public void onScrollStateChanged(AbsListView listview, int scrollState) {}
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        TradeRecordEntity tradeRecordEntity = (TradeRecordEntity)adapterView.getItemAtPosition(i);
        Intent intent = new Intent(oThis,TradeRecordInfoActivity.class);
        intent.putExtra("statementid",tradeRecordEntity.getId());
        startActivity(intent);
    }

    private void loadData(){
        if(!isLastPage) {
            TradeRecordParameters tradeRecordParameters = new TradeRecordParameters();
            tradeRecordParameters.setToken(LoginConfig.getLoginConfig().getToken());
            tradeRecordParameters.setUsername(LoginConfig.getLoginConfig().getShopMember().getUsername());
            tradeRecordParameters.setShopid(LoginConfig.getLoginConfig().getShopMember().getShop_id());
            tradeRecordParameters.setPagesize(pagesize);
            tradeRecordParameters.setPageindex(pageindex);
            new BaseTask<TradeRecordRequest, TradeRecordResponse>(this, TradeRecordRequest.class, TradeRecordResponse.class, tradeRecordParameters, new BaseLiteners.BaseResponseListener() {
                @Override
                public void OnFail(BaseException e) {
                    showToast(e.getErrmsg());
                }

                @Override
                public void OnSuccess(BaseResponse o) {
                    TradeRecordResponse tradeRecordResponse = (TradeRecordResponse) o;
                    pagecount = tradeRecordResponse.getPagecount();
                    if (tradeRecordResponse.getList().size() > 0) {
                        pageindex = tradeRecordResponse.getPageindex() + 1;
                        if (pageindex > pagecount) isLastPage = true;
                        if (adapter == null) {
                            adapter = new TradeRecordAdapter(oThis, tradeRecordResponse.getList());
                            lv.setAdapter(adapter);
                        } else {
                            adapter.addItems(tradeRecordResponse.getList());
                            adapter.notifyDataSetChanged();
                        }
                        ((LinearLayout)findViewById(R.id.ll_nodata)).setVisibility(View.GONE);
                    }else{
                        ((LinearLayout)findViewById(R.id.ll_nodata)).setVisibility(View.VISIBLE);
                    }
                }
            }, true).execute();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.menu_trade_record_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
